#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iterator>

#include "rpsrandom.h"
#include "rpssequence.h"


//We want our functions to have an output as 0, 1, or 2. Inputs can be the past data
int temporary(){ //temporary function to go only rock
	return 0;
}

//Initialize random choice function
int randomRPS();
int sequenceRPS(int prevmove);

int frequency(int &moves, std::vector<char> &phistory, std::vector<char> &chistory) { //looks at player frequencies. Doesn't work yet
	srand(time(NULL));
	double r= (double) rand()/RAND_MAX;
	//std::cout << "Moves:" << r <<std::endl;

	if (moves==0) {
		if (r<0.3333333333) {return 0;}
		else if (r<0.6666666666) {return 1;}
		else {return 2;}
	} else {
		int rocks=0;
		int papers=0;
		int scissors=0;
		for (int i=0; i<moves; i++) {
			if (phistory[i]==0) {rocks++;}
			else if (phistory[i]==1) {papers++;}
			else if (phistory[i]==2) {scissors++;}
			std::cout << phistory[i] << " ";
		}
		std::cout << std::endl;
		if (moves*r<rocks) {return 2;}
		else if (moves*r<rocks+scissors) {return 0;}
		else {return 1;}
	
	}
}


std::string showrps(int &move) { //saves some lines
	if (move==0) {
		return "R";
	} else if (move==1) {
		return "P";
	} else if (move==2) {
		return "S";
	}
}



//to do: have two computers play each other

int main(int argc, char** argv) {
	std::string input;
	bool letsplay=1; //keep playing until user quits
	std::vector<int> wtl (3,0); //win-tie-loss record. Don't play more than 2^31 times
	int pmove; //player's move. Rock is 0, Paper is 1, Scissors is 2.
	int cmove; //computer's move.
	int prevmove=0; //initialize player's previous move to 0;

	int moves=0; //number of moves elapsed
	std::vector< char > phistory; //history of pmove and cmove;
	std::vector< char > chistory;
	std::vector< double > reaction; //reaction times. Unused as of now.
	int streak=0; //for trashtalking purposes




	std::cout << "Choose Algorithm: 1 for random, 2 for sequence" << std::endl;
	int algo;
	std::cin >> algo;
	std::cout << std::endl;

	
	while (letsplay) {
		

		std::cout << "q to quit, r, p, or s to play" << std::endl;
		getline(std::cin, input);

		if (input == "q" or input =="Q") { //to quit
			letsplay=0;
			std::cout << "Let's play again sometime!" << std::endl;
			//want to add extra farewells? Maybe "I'll get you next time!" if CPU lost.
		} else {
			//Playing the game
			if (input == "r" or input == "R") {
				pmove=0;
			} else if (input == "p" or input =="P") {
				pmove=1;
			} else if (input == "s" or input =="S") {
				pmove=2;
			} else {
				std::cout << "Why are you trying to mess up our project?" << std::endl;
				pmove=-1;
			}
			if (pmove!=-1) { //A valid move has been played
				if(algo==1){
					cmove=randomRPS(); //modify this line to use different function
				}
				
				if(algo==2){
					cmove=sequenceRPS(prevmove); //modify this line to use different function
				}
        moves++;
				phistory.push_back(pmove);
				chistory.push_back(cmove);    				
				
				if (cmove==pmove) {
					wtl[1]++;
          streak=0;
				} else if (cmove==(pmove+1)%3) {
					wtl[2]++;
          streak++;     
				} else {
					wtl[0]++;
          streak=0;     
				}
							
				std::cout << "You played: " << showrps(pmove) << "  CPU played: " << showrps(cmove) << std::endl;
            
        if (streak==3) {std::cout << "Gotta step up your game!" <<std::endl;}
				else if (streak==6) {std::cout << "Get read like a Dr. Seuss book!" <<std::endl;}
				else if (streak==9) {std::cout << "DEEEEEESTRUUUUCTIIOOOOON" <<std::endl;
            
				std::cout << "Current record: " << wtl[0] << "-" << wtl[1] << "-" << wtl[2] << std::endl;
			
				prevmove=pmove;
				
			}
		}
	}
	return 0;
}
